﻿
/*
Media Auto Embed jQuery Plugin 1.0

http://nasa8x.com

Copyright (c) 2011 Nasa8x

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/
(function ($) {


    $.extend({

        AutoEmbed: {


            tmp: '<div class="media-embed-wrapper"><embed src="{0}" width="{1}" height="{2}" flashvars="{3}" type="{4}" allowscriptaccess="always" allowfullscreen="true" autostart="true" autoplay="true" wmode="transparent"></embed></div>',

            medias: {
                swf: {
                    reg: /[^\.]\.(swf)\b/i
                },
                youtube: {
                    reg: /youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)[&\w;=\+_\-]*/,
                    url: "http://www.youtube.com/v/{0}",
                    flashvars: {
                        fs: 1
                    }
                },
                vimeo: {
                    flashvars: {
                        server: 'vimeo.com',
                        title: 1,
                        portrait: 0,
                        byline: 1
                    },
                    reg: /vimeo.com\/(\d+)($|\/|\b)/i,
                    url: "http://vimeo.com/moogaloop.swf?clip_id={0}"

                },

                megavideo: {
                    flashvars: {
                        autoplay: 0
                    },
                    reg: /megavideo\.com\/\?v=([A-Za-z0-9._%-]*)[&\w;=\+_\-]*/i,
                    url: "http://www.megavideo.com/v/{0}"
                },

                facebook: {

                    reg: /facebook.com\/video\/video\.php\?v=(\d+)($|s+|\/)/i,
                    url: 'http://www.facebook.com/v/{0}'
                },

                metacafe: {
                    flashvars: {
                        autoplay: 'no'
                    },
                    reg: /metacafe\.com\/watch\/(\d+)($|\/)/i,
                    url: "http://www.metacafe.com/fplayer/{0}/player.swf"
                },
                dailymotion: {
                    flashvars: {
                        additionalInfos: 0
                    },
                    reg: /dailymotion.com\/video\/([A-Za-z0-9._%-]*)($|\/)/i,
                    url: "http://www.dailymotion.com/swf/video/{0}"
                },

                clipvn: {
                    reg: /clip.vn\/watch\/[a-zA-Z0-9_%-]+,([A-Za-z0-9._%-]*)($|\/)/i,
                    url: 'http://clip.vn/w/{0}'
                },

                veoh: {
                    flashvars: {
                        version: 'AFrontend.5.7.0.1060',
                        player: 'videodetailsembedded',
                        videoAutoPlay: 0
                    },
                    reg: /veoh\.com\/watch\/([A-Za-z0-9._%-]*)($|\/)/i,
                    url: "http://www.veoh.com/swf/webplayer/WebPlayer.swf?permalinkId={0}&id=anonymous"
                },

                quicktime: {
                    reg: /\b.mov\b/i,
                    type: 'video/quicktime'
                }

            },

            undefined: function () {
                for (var i = 0; i < arguments.length; i++)
                    if (typeof arguments[i] != 'undefined') return false;
                return true;
            },
            format: function (source) {

                var result = source;
                $.each(arguments, function (i, n) {
                    result = result.replace(new RegExp("\\{" + (i - 1) + "\\}", "g"), n);
                });
                return result;
            },

            objectToArgs: function (o) {
                var type = typeof (o);
                var a = [];
                if (type == "object") {
                    for (var key in o) {
                        if (typeof (o[key]) == "object") {
                            this.objectToArgs(o[key]);
                        }
                        else {
                            a.push(key + '=' + encodeURIComponent(o[key]));
                        }

                    }
                    return a.join('&');
                }
                return '';
            },


            embed: function (a, o) {
                var _href = a.attr('href');
                $.each(this.medias, $.proxy(function (i, v) {
                    var match = _href.match(v.reg);
                    if (match) {
                        var url = !this.undefined(v.url) ? this.format(v.url, match[1]) : _href;
                        var t = !this.undefined(v.type) ? v.type : 'application/x-shockwave-flash';
                        var fvars = this.objectToArgs(o);
                        var html = this.format(this.tmp, url, o.width, o.height, fvars, t);
                        a.replaceWith(html);
                        return;
                    }
                }, this));
            }

        }
    });



    $.fn.iembed = function (options) {

        var defaults = {
            width: 640,
            height: 510,
            autoplay: 0
        };

        this.each(function () {

            var m = $(this);
            var o = $.extend(true, {}, defaults, options || {}, $.metadata ? m.metadata() : $.meta ? m.data() : {});

            $.AutoEmbed.embed(m, o);


        });

    };

})(jQuery);